package com.xgry.bpm.api.base.carapply.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xgry.bpm.api.base.carapply.entity.CarApply;
import com.xgry.bpm.api.base.carapply.form.CarApplyForm;
import com.xgry.bpm.api.base.carapply.service.CarApplyService;
import com.xgry.bpm.api.base.carapply.vo.CarApplyVO;
import com.xgry.bpm.api.base.userInfo.entity.UserInfo;
import com.xgry.bpm.common.utils.EasyPoiUtils;
import com.xgry.bpm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

;

/**
 *
 <p>
 * CarApply前端控制器
 *
 </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@RestController
@Api(tags = "CarApply" )
@RequestMapping("/carApply" )
        public class CarApplyController {
    @Autowired
public CarApplyService carApplyService;

/**
* 保存单条
* @param param 保存参数
* @return 是否添加成功
*/
@ApiOperation(value = "保存" , notes = "保存数据到CarApply" )
@ApiImplicitParams({
                @ApiImplicitParam(paramType = "query" , name = "carApplyId" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "applyEmployee" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "applyReason" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "auditContent" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "carApplyState" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "carInfoId" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "createdBy" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "createdTime" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "driverId" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "endLocation" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "endTime" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "isDel" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "revison" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "startLocation" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "startTime" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "updatedBy" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "updatedTime" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "auditEmp" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "remark" , value = "${field.comment}" , required = false ),
                @ApiImplicitParam(paramType = "query" , name = "sendMsg" , value = "${field.comment}" , required = false ),
    })
@RequestMapping(value = "/add.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result addCarApply(@RequestBody CarApplyForm param){
    Result ajaxResponse = new Result();

    if (StringUtils.isEmpty(param)) {
        ajaxResponse.setCode(HttpStatus.BAD_REQUEST.value());
        ajaxResponse.setMsg(HttpStatus.BAD_REQUEST.getReasonPhrase());        ajaxResponse.setMsg("参数不能为空！");
    }
    /**
     * 获取当前用户信息
     */
    UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
    if (userInfo == null) {
        ajaxResponse.setCode(HttpStatus.BAD_REQUEST.value());
        ajaxResponse.setMsg(HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    param.setApplyEmployee(userInfo.getUserName());
    /**
     * 设置默认信息
     */
    param.setIsDel("0");
    param.setRevison(0);
    param.setCarApplyState(0);
    try {
        carApplyService.save(param);
        return Result.success();
    } catch (Exception e) {
        ajaxResponse.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        ajaxResponse.setMsg("用车申请提交失败！！");
        e.printStackTrace();
    }
    return ajaxResponse;
}

/**
* 更新(根据主键id更新)
* @param param 修改参数
* @return 是否更改成功
*/
@ApiOperation(value = "更新数据" , notes = "根据主键id更新CarApply数据" )
@ApiImplicitParams({
                @ApiImplicitParam(paramType = "query" , name = "carApplyId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyEmployee" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyReason" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditContent" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carApplyState" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carInfoId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "driverId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "isDel" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "revison" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditEmp" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "remark" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "sendMsg" , value = "${field.comment}" , required = false, dataType = "" ),
    })
@RequestMapping(value = "/updateById.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result updateCarApplyById(@RequestBody CarApplyForm param){
        if (1 != carApplyService.updateById(param)) {
                    return Result.fail("保存失败");
                }
                return Result.success();
        }


/**
* 删除(根据主键id删除)
* @param  //主键id
* @return 是否删除成功
*/
@ApiOperation(value = "删除数据" , notes = "根据主键id删除CarApply数据" )
@ApiImplicitParam(paramType = "query" , name = "" , value = "Id" , required = true, dataType = "String" )
@RequestMapping(value = "/deleteById.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
 @Transactional
public Result deleteCarApplyById(String[]ids){
                 int result=0;
                for (String id : ids) {
                    carApplyService.deleteById(id);
                    result ++;
                }

                if (result > 0) {
                    return Result.fail("删除失败！");
                }

                return Result.success("操作成功");
        }

/**
* 根据主键id查询单条
* @param // 主键id
* @return 查询结果
*/
@ApiOperation(value = "获取单条数据" , notes = "根据主键id获取CarApply数据" )
@ApiImplicitParam(paramType = "query" , name = "" , value = "Id" , required = true, dataType = "String" )
@RequestMapping(value = "/getById.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result getCarApplyById(@RequestParam(value = "Id" ) String id){
                CarApplyVO result =  carApplyService.selectById(id);
                if (null == result) {
                    return Result.fail("无数据！");
                }
                return Result.success("操作成功",result);
        }

/**
* 查询全部
* @param //param 查询条件
* @return 查询结果
*/
@ApiOperation(value = "全部查询" , notes = "查询CarApply全部数据" )
@ApiImplicitParams({
                @ApiImplicitParam(paramType = "query" , name = "carApplyId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyEmployee" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyReason" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditContent" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carApplyState" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carInfoId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "driverId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "isDel" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "revison" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditEmp" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "remark" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "sendMsg" , value = "${field.comment}" , required = false, dataType = "" ),
    })
@RequestMapping(value = "/queryAll.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result getCarApplyAll(@RequestBody CarApplyForm param){
        List<CarApplyVO> result = carApplyService.selectAll(param);
                if (CollectionUtils.isEmpty(result)) {
                    return Result.fail("无数据！");
                }
                return Result.success("操作成功",result);
}

/**
* 分页查询
* @param //param 查询条件
* @return 查询结果
*/
@ApiOperation(value = "分页查询" , notes = "分页查询CarApply全部数据" )
@ApiImplicitParams({
                @ApiImplicitParam(paramType = "query" , name = "carApplyId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyEmployee" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "applyReason" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditContent" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carApplyState" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "carInfoId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "createdTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "driverId" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "endTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "isDel" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "revison" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startLocation" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "startTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedBy" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "updatedTime" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "auditEmp" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "remark" , value = "${field.comment}" , required = false, dataType = "" ),
                @ApiImplicitParam(paramType = "query" , name = "sendMsg" , value = "${field.comment}" , required = false, dataType = "" ),
    })
@RequestMapping(value = "/getAllApply.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result getCarApplyPage(@RequestBody CarApplyForm param){
        IPage<CarApplyVO> result = carApplyService.CarApplyPage(param);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return Result.fail("无数据！");
        }
        return Result.convertIpageToSuccess(result);
        }

/**
* Excel导入
* @param //param 查询条件
* @return 查询结果
*/
@ApiOperation(value = "Excel导入" , notes = "导入全字段" )
@ApiImplicitParams({
        @ApiImplicitParam(paramType = "query" , name = "" , value = "" , required = false, dataType = "String" ),
        @ApiImplicitParam(paramType = "query" , name = "" , value = "" , required = false, dataType = "String" )
})
@RequestMapping(value = "/importExcel.do" , method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public Result importExcel(@RequestParam MultipartFile file)throws Exception{
            List<CarApply> list= EasyPoiUtils.importExcel(file,0,1,CarApply.class);
            if (CollectionUtils.isEmpty(list)) {
                return Result.fail("无数据");
            }
            return Result.success();
        }
/**
* Excel导出
* @param //param 查询条件
* @return 查询结果
*/
@ApiOperation(value = "Excel导出" , notes = "导出全字段" )
@ApiImplicitParams({
        @ApiImplicitParam(paramType = "query" , name = "current" , value = "页码" , required = false, dataType = "String" ),
        @ApiImplicitParam(paramType = "query" , name = "size" , value = "每页条数" , required = false, dataType = "String" )

})
@RequestMapping(value = "/exportExcel.do" , method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
public void exportExcel(HttpServletResponse response)throws Exception{
 List<CarApply> result= carApplyService.list();
        EasyPoiUtils.exportExcel(result,"列表" ,"报表" , CarApply.class,"明细报表.xls" ,response);

        }


        }

